home *** CD-ROM | disk | FTP | other *** search
/ Maplin Electronics Catalogue 2001 Winter / Maplins Catalogue Winter 2001.iso / JScript / scart.js < prev    next >
Text File  |  2000-10-17  |  9KB  |  239 lines

  1. // scart.js JavaScript Development by Burgeonet.com
  2.  
  3. // ****Begin shopping cart functions.****
  4.  
  5. // ** global variables for the shopping cart.**
  6.  
  7. var itemNum = new Array();
  8. var itemDescr = new Array();
  9. var itemCost = new Array();
  10. var itemQty = new Array();
  11.                                        
  12.  
  13. // **Function to parse the cookie and extract the ordered **
  14. // **items from the string. This in turn triggers the **
  15. // **writeCart() function to display the shopping cart. **
  16.  
  17. function itemsOrdered() {
  18.  if (getCookieData("Scart")) {
  19.  substr0 = getCookieData("Scart")
  20.  cLen = substr0.length
  21.  offset0 = substr0.indexOf("@")
  22.  
  23.  counter = substr0.substring(0,offset0)
  24.  j = 0
  25.   for (i=1; i<=counter; i++) {
  26.   offsetq = eval('offset' + j + '');
  27.   substrq = eval('substr' + j + '');
  28.  
  29.   eval('ind' + i + ' = offsetq + 1');
  30.   eval('substr' + i + ' = substrq.substring(ind' + i + ',cLen)');
  31.   eval('offset' + i + ' = substr' + i + '.indexOf("^")');
  32.   eval('item' + i + ' = substr' + i + '.substring(0,offset' + i + ')');
  33.  
  34.   eval('catInd' + i + ' = item' + i + '.indexOf("`")');
  35.   eval('itemCat' + i + ' = item' + i + '.substring(0,catInd' + i + ')');
  36.  
  37.   eval('descrInd' + i + ' = item' + i + '.indexOf("~")');
  38.   eval('itemDes' + i + ' = item' + i + '.substring((catInd' + i + ' + 1),descrInd' + i + ')');
  39.  
  40.   eval('priceInd' + i + ' = item' + i + '.indexOf("½")');
  41.   eval('itemPr' + i + ' = item' + i + '.substring((descrInd' + i + ' + 2),priceInd' + i + ')'); 
  42.   
  43.   eval('itemQt' + i + ' = item' + i + '.substring((priceInd' + i + ' + 1),offset' + i + ')'); 
  44.   eval('orderDetail(i,itemCat' + i + ',itemDes' + i + ',itemPr' + i + ',itemQt' + i +')');
  45.  
  46.  
  47.   j++
  48.  
  49.  }
  50.  writeCart();
  51.  }
  52. }
  53.  
  54. // **Builds an array of the items to load the cart.**
  55.  
  56. function orderDetail(seq,num,descr,cost,Qty) {
  57.  itemNum[seq]   = num
  58.  itemDescr[seq] = descr
  59.  itemCost[seq]  = cost
  60.  itemQty[seq]  = Qty
  61. }
  62.  
  63. // ** Function to write the shopping cart details **
  64. // ** into the table on the shopping cart page.
  65.  
  66.  
  67. function writeCart() {
  68.  
  69.  var ordFrm  =  ' <TABLE WIDTH="466" BORDER="0" CELLSPACING="0" CELLPADDING="0"'
  70.  ordFrm +=  '      BGCOLOR="#000096"> '
  71.  ordFrm +=  '        <TR> '
  72.  ordFrm +=  '          <TD WIDTH="147" HEIGHT="21"><FONT FACE="verdana,Arial,helvetica"'
  73.  ordFrm +=  '             SIZE="2" COLOR="#FFFFFF"><B> Shopping Basket</B></FONT></TD> '
  74.  ordFrm +=  '          <TD ALIGN="RIGHT" WIDTH="319" HEIGHT="21"> </TD> '
  75.  ordFrm +=  '        </TR> '
  76.  ordFrm +=  '     </TABLE><BR> '
  77.  ordFrm += '<form name="shopCart">'
  78.  ordFrm +=  '     <TABLE WIDTH="466" BGCOLOR="#000000" BORDER="1" BORDERCOLOR="#000096"'
  79.  ordFrm +=  '      CELLPADDING="0" CELLSPACING="0"> '
  80.  ordFrm +=  '        <TR> '
  81.  ordFrm +=  '          <TD BGCOLOR="#FFFFFF"> '
  82.  ordFrm +=  '             <TABLE WIDTH="466" BGCOLOR="#000096" BORDER="0" CELLPADDING="0"'
  83.  ordFrm +=  '              CELLSPACING="0"> '
  84.  ordFrm +=  '                <TR VALIGN="TOP"> '
  85.  ordFrm +=  '                  <TD WIDTH="60" VALIGN="TOP" ALIGN="LEFT"><FONT'
  86.  ordFrm +=  '                     FACE="verdana,Arial,helvetica" SIZE="1" COLOR="#FFFFFF">'
  87.  ordFrm +=  '                     <B> Order<BR> Code</B></FONT></TD> '
  88.  ordFrm +=  '                  <TD WIDTH="140" VALIGN="TOP" ALIGN="LEFT"><FONT'
  89.  ordFrm +=  '                     FACE="verdana,Arial,helvetica" SIZE="1"'
  90.  ordFrm +=  '                     COLOR="#FFFFFF"><B>Description</B></FONT></TD> '
  91.  ordFrm +=  '                  <TD COLSPAN="2" WIDTH="60" VALIGN="TOP" ALIGN="CENTER"><FONT'
  92.  ordFrm +=  '                     COLOR="#FFFFFF" FACE="verdana,Arial,helvetica" SIZE="1"><B>Qty</B></FONT></TD> '
  93.  ordFrm +=  '                  <TD WIDTH="70" VALIGN="TOP" ALIGN="RIGHT"><FONT'
  94.  ordFrm +=  '                     FACE="verdana,Arial,helvetica" SIZE="1" COLOR="#FFFFFF"><B>inc.VAT'
  95.  ordFrm +=  '                     <BR>each</B></FONT></TD> '
  96.  ordFrm +=  '                  <TD WIDTH="60" ALIGN="RIGHT" VALIGN="TOP"><FONT'
  97.  ordFrm +=  '                     FACE="verdana,Arial,helvetica" SIZE="-2"'
  98.  ordFrm +=  '                     COLOR="#FFFFFF"><B>Total</B></FONT></TD> '
  99.  ordFrm +=  '                  <TD VALIGN="TOP" ALIGN="CENTER" WIDTH="80"><FONT'
  100.  ordFrm +=  '                     FACE="verdana,Arial,helvetica" SIZE="1"'
  101.  ordFrm +=  '                     COLOR="#FFFFFF"><B>Remove</B></FONT></TD> '
  102.  ordFrm +=  '                </TR> '
  103.  ordFrm +=  '             </TABLE> '
  104.  
  105.  ordFrm +=  '             <TABLE WIDTH="466" BORDER="0" CELLPADDING="0"'
  106.  ordFrm +=  '              CELLSPACING="0" BGCOLOR="#FBCE82"> '
  107.  ordFrm +=  '                <TR> '
  108.  ordFrm +=  '                  <TD></TD> '
  109.  ordFrm +=  '                </TR> '
  110.  ordFrm +=  '             </TABLE> '
  111.  
  112.  
  113.   for (i = 1; i <= counter; i++) {
  114.  ordFrm +=  '             <TABLE WIDTH="466" BGCOLOR="#FEF0D8" BORDER="0" CELLPADDING="0"'
  115.  ordFrm +=  '              CELLSPACING="0"> '
  116.  ordFrm +=  '                <TR> '
  117.  ordFrm +=  '                  <TD WIDTH="60"><FONT FACE="verdana,Arial,helvetica" SIZE="-2"'
  118.  ordFrm +=  '                     COLOR="#330099"><B> ' + itemNum[i] + '</B></FONT></TD> '
  119.  ordFrm +=  '                  <TD WIDTH="140"><FONT FACE="verdana,Arial,helvetica"'
  120.  ordFrm +=  '                     SIZE="-2">' + itemDescr[i] + '</FONT></TD> '
  121.  ordFrm +=  '                  <TD WIDTH="45" ALIGN="CENTER"><FONT'
  122.  ordFrm +=  '                     FACE="verdana,Arial,helvetica" SIZE="1">' + itemQty[i] + '</FONT></TD> '
  123.  ordFrm +=  '                  <TD WIDTH="70" ALIGN="RIGHT"><FONT FACE="verdana,Arial,helvetica"'
  124.  ordFrm +=  '                     SIZE="-2">£' + itemCost[i] + '</FONT></TD> '
  125.  ordFrm +=  '                  <TD WIDTH="60" ALIGN="RIGHT"><FONT FACE="verdana,Arial,helvetica"'
  126.  ordFrm +=  '                     SIZE="-2">£' + fix(itemCost[i]  *  itemQty[i]) + '</FONT></TD> '
  127.  ordFrm +=  '                  <TD WIDTH="80" ALIGN="RIGHT"><A href="javascript:clearIt('+i+')"><IMG'
  128.  ordFrm +=  '                     SRC="remove.gif" WIDTH="60" HEIGHT="15" BORDER="0"></A></TD> '
  129.  ordFrm +=  '                </TR> '
  130.  ordFrm +=  '             </TABLE> '
  131.   ordFrm +=  '             <TABLE WIDTH="466" BORDER="0" CELLPADDING="0"'
  132.   ordFrm +=  '              CELLSPACING="0" BGCOLOR="#FBCE82"> '
  133.   ordFrm +=  '                <TR> '
  134.   ordFrm +=  '                  <TD></TD> '
  135.   ordFrm +=  '                </TR> '
  136.   ordFrm +=  '             </TABLE> '
  137.  
  138.   }
  139.  
  140.  ordFrm +=  '             <TABLE WIDTH="466" BGCOLOR="#FEF0D8" BORDER="0" CELLPADDING="0"'
  141.   ordFrm +=  '              CELLSPACING="0"> '
  142.   ordFrm +=  '                <TR> '
  143.   ordFrm +=  '                  <TD WIDTH="60"> </TD> '
  144.   ordFrm +=  '                  <TD WIDTH="140"> </TD> '
  145.   ordFrm +=  '                  <TD WIDTH="45" ALIGN="CENTER"> </TD> '
  146.   ordFrm +=  '                  <TD WIDTH="70" ALIGN="RIGHT"><FONT FACE="verdana,Arial,helvetica"'
  147.   ordFrm +=  '                     SIZE="-2" COLOR="#000066">Sub Total</FONT></TD> '
  148.   ordFrm +=  '                  <TD WIDTH="60" ALIGN="RIGHT"><FONT FACE="verdana,Arial,helvetica"'
  149.   ordFrm +=  '                     SIZE="-2">£' + update() + '</FONT></TD> '
  150.   ordFrm +=  '                  <TD WIDTH="80" ALIGN="RIGHT"> </TD> '
  151.   ordFrm +=  '                </TR> '
  152.  
  153.   ordFrm +=  '             </TABLE>'
  154.   ordFrm +=  '             <TABLE WIDTH="466" BORDER="0" CELLPADDING="0"'
  155.   ordFrm +=  '              CELLSPACING="0" BGCOLOR="#FBCE82"> '
  156.   ordFrm +=  '                <TR> '
  157.   ordFrm +=  '                  <TD></TD> '
  158.   ordFrm +=  '                </TR> '
  159.   ordFrm +=  '             </TABLE> '
  160.  
  161. ordFrm +=  '             <TABLE WIDTH="466" BGCOLOR="#FEF0D8" BORDER="0" CELLPADDING="0"'
  162.  ordFrm +=  '              CELLSPACING="0"> '
  163.  ordFrm +=  '                <TR VALIGN="MIDDLE"> '
  164.  ordFrm +=  '                  <TD ALIGN="CENTER" VALIGN="MIDDLE" WIDTH="120"><A'
  165.  ordFrm +=  '                     HREF="javascript:history.go(-1)"><IMG SRC="return.gif" WIDTH="60" HEIGHT="15"'
  166.  ordFrm +=  '                     BORDER="0"></A></TD> '
  167.  ordFrm +=  '                  <TD ALIGN="CENTER" VALIGN="MIDDLE" WIDTH="149"><A HREF="checkout.htm"><IMG'
  168.  ordFrm +=  '                     SRC="checkout.gif" WIDTH="110" HEIGHT="15" BORDER="0" ALIGN="BOTTOM"></A></TD> '
  169.  ordFrm +=  '                  <TD ALIGN="CENTER" VALIGN="MIDDLE" WIDTH="200"><A HREF="javascript:killCart()"><IMG'
  170.  ordFrm +=  '                     SRC="empty.gif" WIDTH="145" HEIGHT="14" BORDER="0"></A></TD> '
  171.  ordFrm +=  '                </TR> '
  172.  ordFrm +=  '             </TABLE> </TD> '
  173.  ordFrm +=  '        </TR> '
  174.  ordFrm +=  '     </TABLE>'
  175.   ordFrm += '</form> '
  176.   document.write(ordFrm);
  177.   document.close();
  178. }
  179.  
  180. // ** Function to delete a line item upon user request. **
  181.  
  182. function clearIt(num) {
  183.  itemNum[num] = "item"
  184.  rewriteCookie(num)
  185. }
  186.  
  187. // **Function to rewrite the cookie when the user **
  188. // **deletes a line item from the shopping cart. **
  189.  
  190. function rewriteCookie(num) {
  191.  dataUpdate = ""
  192.  for (i=1; i<=counter; i++) {
  193.    if (itemNum[i] != "item") {
  194.     dataUpdate += itemNum[i] + '`' + itemDescr[i] + '~$' + itemCost[i] + '½' + itemQty[i] + '^'
  195.    }
  196.  }
  197.  counter = counter - 2
  198.  cookData = dataUpdate
  199.  setCookieData("Scart", cookData, expdate.toGMTString())
  200.  history.go(0)
  201. }
  202.  
  203. // ****End shopping cart detail section.****
  204.  
  205. // ****Start of code section to display subtotal.****
  206.  
  207. function update() {
  208.  if (getCookieData("Scart")) {
  209.   var sub_total = 0;
  210.   for (i=1; i<itemNum.length; i++)
  211.    eval('sub_total += parseFloat(itemCost[' + i + '] * itemQty[' + i + ']);');
  212.     //document.shopCart.subtotal.value= fix(sub_total);
  213.      return + fix(sub_total);
  214.  }
  215. }
  216.  
  217.  
  218. function fix(num) {
  219.  string = "" + num;
  220.  if (string.indexOf('.') == -1)
  221.   return string + '.00';
  222.  seperation = string.length - string.indexOf('.');
  223.  if (seperation > 3)
  224.   return string.substring(0,string.length-seperation+3);
  225.  else if (seperation == 2)
  226.   return string + '0';
  227.  return string;
  228. }
  229.  
  230. // **Function to clear the shopping cart.**
  231.  
  232. function killCart() {
  233.  killCookie("Scart")
  234.  history.go(-1)
  235. }
  236.  
  237.  
  238.  
  239.